package com.monster.mvc.controller;

import com.monster.mvc.model.entity.UserEntity;
import com.monster.mvc.model.vo.UserAddReqVO;
import com.monster.mvc.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.SimpleFormatter;

/**
 * description:
 *
 * @author Yuanml on 2020/12/31 08:24
 */
@Controller
@RequestMapping("/user")
public class UserController {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
    @Resource
    private UserService userService;
    @RequestMapping(value = "/add.do", method = RequestMethod.POST)
    public ModelAndView addUser(UserAddReqVO reqVO) {
        ModelAndView mv = new ModelAndView();
        UserEntity userEntity = new UserEntity();
        userEntity.setName(reqVO.getName());
        userEntity.setPassword(reqVO.getPassword());
        try {
            userEntity.setBirth(simpleDateFormat.parse(reqVO.getBirth()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        userService.addUser(userEntity);
        mv.addObject("tips", "注册成功");
        mv.setViewName("result");
        return mv;
    }
}
